package domain;

import javax.persistence.Entity;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import java.util.Date;

@Entity
public class PasswordReset extends AbstractEntity<PasswordReset> {
    @OneToOne
    User user;
    String randomKey;
    @Temporal(javax.persistence.TemporalType.DATE)
    Date created;

    public PasswordReset() {
    }

    public PasswordReset(User user, Integer lengthMin, Integer lengthMax) {
        this.user = user;
        randomKey = randomString(lengthMin, lengthMax);
    }

    private static Integer randomInt(
            int min, int max) {
        return (int) (Math.random() * (max - min) + min);
    }

    private static String randomString(int min, int max) {
        int num = randomInt(min, max);
        byte b[] = new byte[num];
        for (int i = 0; i < num; i++) {
            b[i] = (byte) randomInt('a', 'z').intValue();
        }
        return new String(b);
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getRandomKey() {
        return randomKey;
    }

    public void setRandomKey(String randomKey) {
        this.randomKey = randomKey;
    }

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }
}
